<template>
	<view>
		<view class="title other">问吧获取以下权限</view>
		<view class="title">获取你的公开信息(昵称，头像等)</view>
		<button type='primary' open-type="getUserInfo" @getuserinfo="Login">登录</button>
	</view>
</template>
<script>
	import api from "@/common/api.js";
	import {
		baseURL
	} from "@/common/http.js";
	export default {
		data() {
			return {
				code: '',
				ip: '',
				cheng: '',
				data: {},
				gender: ''
			};
		},
		methods: {
			// 注册
			register(data) {
				let _this = this
				api.zhuce(data).then(res => {
					if (res.code == 200) {
						uni.setStorageSync('message', res.date)
						setTimeout(() => {
							uni.navigateTo({
								url: '../noteLogin/noteLogin?bindCode=' + true + "&id=" + res.date[0].id
							})
						}, 500)
					} else if (res.code === 203) {
						uni.setStorageSync('message', res.date)
						if (res.date[0].call && res.date[0].call != '') {
							setTimeout(() => {
								uni.reLaunch({
									url: "../index/index"
								})
							}, 500)
						} else {
							uni.navigateTo({
								url: '../noteLogin/noteLogin?id=' + res.date[0].id
							})
						}
					}
				}).catch(err => {
					console.log(err)
				})
				uni.hideLoading({})
			},
			// 登录
			Login(e) {
				e.detail.userInfo.gender === 1 ? this.gender = "男" : this.gender = '女'
				if (e.detail.userInfo) {
					uni.login({
						success: (res) => {
							this.code = res.code
							uni.getUserInfo({
								success: (res) => {
									this.data = {
										api: 'namezhu',
										wx: this.code,
										signature: res.signature,
										rawData: res.rawData,
										encryptedData: res.encryptedData,
										iv: res.iv,
										cheng: this.cheng,
										name: e.detail.userInfo.nickName,
										sex: this.gender
									}
									this.register(this.data)
								}
							})
						}
					})
				} else {
					uni.showToast({
						title: "您点击了拒绝授权",
						icon: 'none',
						duration: 1000
					})
				}
			}
		},
		onShareAppMessage(options) {
			let shareObj = {
				success: (res) => {
					if (res.errMsg == 'shareAppMessage:ok') {
						uni.showToast({
							title: '分享成功',
							icon: 'success',
							duration: 1500
						})
					}
				},
				fail: () => {
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						uni.showToast({
							title: '取消分享',
							icon: 'none',
							duration: 2000
						})
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				},
				complete: () => {
					// 转发结束之后的回调（转发成不成功都会执行）
				}
			}
		},
		onLoad() {
			uni.request({ // 获取ip
				url: 'http://ip-api.com/json/?lang=zh-CN',
				success: (e) => {
					console.log(e)
					this.ip = e.data.query
					this.cheng = e.data.country + '-' + e.data.regionName + '-' + e.data.city
				}
			})
		}
	}
</script>

<style lang="scss">
	.other {
		font-size: 34rpx;
		font-weight: bold;
	}

	.title {
		width: 680rpx;
		margin: 0 auto;
		padding: 20rpx 0;
		color: #333;
	}

	button {
		width: 690rpx;
		height: 100rpx;
		line-height: 100rpx;
		background: #46BB36;
		color: #fff;
		text-align: center;
		font-size: 30rpx;
		margin: 40% auto 0;
	}
</style>
